package com.xm.web.controller.business;

import com.xm.common.entity.R;
import com.xm.web.entity.PageResult;
import com.xm.web.entity.request.UserRequest;
import com.xm.web.entity.response.UserResponse;
import com.xm.web.service.ITbUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户管理[员工管理]
 *
 * @author yousj
 * @since 2021/6/12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/user/employee")
@Api(tags = "员工")
public class EmployeeController {

    private final ITbUserService userService;

    @ApiOperation(value = "列表")
    @GetMapping("/list")
    @Cacheable(value = "userSelectByPageCache", key = "'userSelectByPageCache#600#' + " +
            "#userRequest.pageNum + '_'+ #userRequest.pageSize + '_' +#userRequest.nickname")
    public R<PageResult<UserResponse>> list(UserRequest userRequest) {
        return R.isOk(userService.pageList(userRequest));
    }

    @ApiOperation(value = "详情")
    @GetMapping("/details/{id}")
    public R details(@PathVariable Integer id) {
        return R.isOk(userService.selectById(id));
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    @CacheEvict(value = "userSelectByPageCache", allEntries = true)
    public R add(@RequestBody @Valid UserRequest userRequest) {
        return R.isOk(userService.save(userRequest));
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    @CacheEvict(value = "userSelectByPageCache", allEntries = true)
    public R update(@RequestBody UserRequest userRequest) {
        return R.isOk(userService.update(userRequest));
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{id}")
    @CacheEvict(value = "userSelectByPageCache", allEntries = true)
    public R delete(@PathVariable Integer id) {
        return R.isOk(userService.removeById(id));
    }

}
